package com.itheima.jobs;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.RedisConstant;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Set;

/**
 * 自定义任务，定时清理垃圾图片
 */
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderSettingService orderSettingService;

    //清理图片
    public void clearImg(){
        System.out.println("定时清理垃圾图片");
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if(set != null){
            for (String fileName : set) {
                System.out.println("定时清理垃圾图片： " + fileName);
                //根据图片名称从七牛云服务器删除文件
                QiniuUtils.deleteFileFromQiniu(fileName);
                //从redis集合中删除图片名称
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            }
        }
    }

    //定时清理预约设置
    public void clearOrderSettingData(){
        System.out.println("每月最后一天定时清理预约设置图片clear");
        //获取当前月,根据当前月调用mapper删除预约设置的数据
        try {
            String thisMonth = DateUtils.parseDate2String (new Date (), "yyyy-MM");
            orderSettingService.clearOrderSettingData(thisMonth);
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }
}
